package inventory.stockManagement.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import common.controller.IssueSlipController;

import model.GinItems;
import model.IssueSlipItem;
import model.Material;

import utility.Constants;
import utility.ViewUtilities;
import dao.GinDao;
import dao.GinItemsDao;
import dao.IssueSlipDao;
import dao.IssueSlipItemDao;
import dao.MaterialMasterDao;
import dao.MaterialSpecificationDao;
import dto.Gin;
import dto.IssueSlip;
import dto.MaterialMaster;
import dto.MaterialSpecification;
import implementor.GinImplementor;
import implementor.GinItemsImplementor;
import implementor.IssueSlipImplementor;
import implementor.IssueSlipItemImplementor;
import implementor.MaterialMasterImplementor;
import implementor.MaterialSpecificationImplementor;
import inventory.stockManagement.view.OpenIssueSlipView;



public class LiveIssueSlipController implements ListSelectionListener,ActionListener
{
	private IssueSlip model;
	private OpenIssueSlipView view;
	
	public LiveIssueSlipController(OpenIssueSlipView view,IssueSlip model) 
	{
		super();
		this.model = model;
		this.view = view;
		view.addlistlistener(this);
		try {
			IssueSlipController cntrl = new IssueSlipController(view.getIssueSlipView(),null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		setListSlip();
	}
	
	public LiveIssueSlipController()
	{
		
	}
	
//*******************************************************************************

	private void setListSlip()
	{
		IssueSlipDao imp=new IssueSlipImplementor();
		IssueSlip[] slipArray = null;
		try {
			slipArray = imp.retreiveArray("status", Constants.ACTIVE+"");
		} catch (Exception e) 
			{
			// TODO Auto-generated catch block
			e.printStackTrace();
			}
		
		String[] items=new String[slipArray.length];
		
		for(int i=0;i<items.length;i++)
			items[i]=slipArray[i].getAutoissueSlipNo()+","+slipArray[i].getGenratedDate();
		
		ViewUtilities.setList(view.getList(), items);
	}
	
	private void setIsuueSlipItemTable(String selection)
		{
			IssueSlipItemDao items=new IssueSlipItemImplementor();
			Material m=null;
			IssueSlipItem[] issueItems = null;
			try 
				{
				issueItems = items.retreiveArray("slipNo",selection);
				} catch (Exception e) 
				{
					e.printStackTrace();
		        }
		 for(int i=0;i<issueItems.length;i++)
		 {
			m=new Material(issueItems[i].getmCode(),issueItems[i].getSpecCode());
			issueItems[i].setModelmaterial(m);
		 }
		
		JTable temp=view.getTable();
		int sr=0;
		DefaultTableModel dm=(DefaultTableModel) temp.getModel();
		
		for(IssueSlipItem gin:issueItems)
		{
			dm.addRow(new Object[]{sr,gin.getModelmaterial().getMname(),gin.getModelmaterial().getCurrentSpecs().getmSpecs(),gin.getModelmaterial().getUnit(),gin.getRequiredQuant(),gin.getIssueQuant()});
			sr++;
		}
	}
	
	public IssueSlip getModel() 
	{
		return model;
	}

	public void setModel(IssueSlip model) {
		this.model = model;
	}

	public OpenIssueSlipView getView() {
		return view;
	}

	public void setView(OpenIssueSlipView view) {
		this.view = view;
	}
	
	
	public void valueChanged1(ListSelectionEvent arg0) 
	{
		 if (arg0.getValueIsAdjusting())
			 ;
		 String selection= view.getList().getSelectedValue().toString();	
	}

	//************************************************************************************************************ 
	
	@Override
	public void valueChanged(ListSelectionEvent arg0) 
	{
		if (arg0.getValueIsAdjusting());
		
		else
		 {
			 String selection= view.getList().getSelectedValue().toString();
			 
			 selection=selection.split(",")[0];
			 
			 setIsuueSlipItemTable(selection);
		 }
	}

	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		
	}
}
